package com.atguigu.gmall.thread;

import javax.sound.midi.Track;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 李旭
 * @Time: 11:24
 * @Description:
 *
 *   道格.林  Java 1.8写出来的  异步编排  类  CompletableFuture
 *      50+方法
 *
 *
 */
public class CompletableFutureDemo {

    // A 3  B  4 C 5
    public static void main(String[] args) {

/*        for (int j = 0; j < 10 ; j++) {
            //多线程之间 串行关系
            CompletableFuture.runAsync(() -> {
                for (int i = 0; i < 3; i++) {
                    System.out.println("A");
                }
            }).thenAccept(t -> {
                for (int i = 0; i < 4; i++) {
                    System.out.println("B");
                }
            }).thenAccept(t -> {
                for (int i = 0; i < 5; i++) {
                    System.out.println("C");
                }
            });
        }*/


        //1:创建一个多线程  Runnable  Callable
 /*       CompletableFuture.runAsync(() -> {
            int i = 0 ;
            i=1;
            i++;
            System.out.println(i);
        });
        CompletableFuture.supplyAsync(() -> {
            int i = 0 ;
            i=1;
            i++;
            System.out.println(i);
            return i;
        });*/

        //2:  计算完成时回调方法   去线程池   5个线程   1 2 3 4 5
/*        CompletableFuture.supplyAsync(() -> {//第一个任务 老
            int i = 0 ;
            i=1;
            i++;
            int j = i/0;
            System.out.println("i:" + i);
            return i;// 获取1号线程
        }).whenCompleteAsync((t,u) -> {//第2个任务 老
            //降级处理
            System.out.println("t=i:" + t);
            System.out.println("u:异常：" + u);
        }).exceptionally((u) -> {

            System.out.println("exceptionally:u:异常：" + u);
            return 0;
        });*/
        //3:串行
   /*     CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("串行1");
            return "123";
        }).thenApplyAsync((t) -> {
            System.out.println("t:" + t);
            return t;
        }).whenCompleteAsync((t,u) -> {
            System.out.println("whenCompleteAsync:t:" + t);
            System.out.println("whenCompleteAsync:u:" + u);
        }).join();*/
        //4:并行
        CompletableFuture<String> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("并行1");
            return "123";
        });
        CompletableFuture<String> completableFuture2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("并行2");
            return "456";
        });
        CompletableFuture<String> completableFuture3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("并行3");
            return "789";
        });
        //Main  等会
        CompletableFuture.allOf(completableFuture1,completableFuture2,completableFuture3)
                .join();

    }}
